package edu.ccut.saturn.studio.database.model;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.graphics.Image;

/**
 * 描述树的根节点
 * 
 * @author 谷钰
 * @version 1.0 2008.12.02
 */
public class SaturnTree {

	private String name = "";

	private String description = "";

	private SaturnTree parent = null;

	private ArrayList<SaturnTree> children = new ArrayList<SaturnTree>();

	private boolean isOpen = false;
	
	public SaturnTree(String name) {
		this.name = name;
	}
	
	public SaturnTree(String name, String description) {
		this.name = name;
		this.description = description;
	}
	
	public void expand() throws Exception{
		
	}
	
	public Image getImage() {
		return null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParent(SaturnTree parent) {
		this.parent = parent;
	}

	public SaturnTree getParent() {
		return this.parent;
	}

	public String toString() {
		return getName();
	}

	@SuppressWarnings("unchecked")//有问题
	public Object getAdapter(Class clazz) {
		return this;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public ArrayList<SaturnTree> getChildren() {
		return this.children;
	}

	public void addChild(SaturnTree child) {
		this.children.add(child);

		child.setParent(this);
	}

	public void addChildren(Collection<? extends SaturnTree> children) {
		if (children != null) {
			for (SaturnTree child : children) {
				addChild(child);
			}
		}
	}

	public void removeChild(SaturnTree child) {
		child.setParent(null);
		child.removeChildren();
	}

	public void removeChildren() {
		if (!this.children.isEmpty()) {
			for (SaturnTree child : this.children) {
				this.removeChild(child);
			}
		}
		
		this.children.clear();
	}

	public boolean hasChildren() {
		return !this.children.isEmpty();
	}

	public boolean isOpen() {
		return isOpen;
	}

	public void setOpen(boolean isOpen) {
		this.isOpen = isOpen;
	}
}